/*
 * Copyright (c) 2018.
 * 北京九思金信科技有限公司对该文件内容保留所有权利。
 * 未经许可，不得私自拷贝、传播、发布、引用该文件内容，违者将被追究法律责任。
 */

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import JoysUI from 'joys-ui'
import store from './store/store'
import http from './utils/http'
import echarts from 'echarts'
import FrameBus from 'joys-frame-bus'
import VueGridLayout from 'vue-grid-layout'

Vue.prototype.$echarts = echarts
Vue.use(http)
Vue.use(ElementUI)
Vue.use(JoysUI)
Vue.use(FrameBus)
Vue.use(VueGridLayout)

Vue.config.productionTip = false

Vue.prototype.$baseUrl = ''
//清理缓存

for (let i = localStorage.length - 1; i >= 0; i--) {
  const key = localStorage.key(i)
  if (
    key.startsWith('joys-auth.') ||
    key.startsWith('AUTH-TOKEN') ||
    key === 'current-theme' ||
    key.startsWith('JOYS_FAVORITE_PAGE_SIZE')
  ) {
    continue
  }
  localStorage.removeItem(key)
}

// 格式化时间的过滤器
Vue.filter('dateFormat', originVal => {
  var myDate = new Date().getTime()
  var gapDate = myDate - originVal
  const dd = new Date(myDate)
  const dt = new Date(originVal)
  const y = dt.getFullYear()
  const m = (dt.getMonth() + 1).toString().padStart(2, '0')
  const d = dt
    .getDate()
    .toString()
    .padStart(2, '0')
  const ds = dd
    .getDate()
    .toString()
    .padStart(2, '0')

  const hh = dt
    .getHours()
    .toString()
    .padStart(2, '0')
  const mm = dt
    .getMinutes()
    .toString()
    .padStart(2, '0')
  const ss = dt
    .getSeconds()
    .toString()
    .padStart(2, '0')

  if (gapDate < 86400000) {
    if (ds != d) {
      return `昨天 ${hh}:${mm}:${ss}`
    } else {
      return `今天 ${hh}:${mm}:${ss}`
    }
  } else {
    return `${y}/${m}/${d} ${hh}:${mm}:${ss}`
  }
})

const CURRENT_THEME_PROP = 'current-theme'

if (!localStorage.getItem(CURRENT_THEME_PROP)) {
  localStorage.setItem(CURRENT_THEME_PROP, 'theme-black')
}
import('joys-ui/theme/' + localStorage.getItem(CURRENT_THEME_PROP) + '/index.css').then(() => {
  /* eslint-disable no-new */
  new Vue({
    el: '#app',
    router,
    store,
    components: {App},
    template: '<app/>'
  })
})

